package com.rzhd.coursepatriarch.common.utils;

import android.content.Context;
import android.view.Gravity;
import android.widget.Toast;

import com.rzhd.coursepatriarch.common.base.BaseApplication;

/**
 *
 * @author yql
 * @date 2018/4/17
 */

public class ToastUtils {

    public static void longToast(String content) {
        Toast.makeText(BaseApplication.getInstance(), content, Toast.LENGTH_LONG).show();
    }

    public static void longToast(int resourceId) {
        Toast.makeText(BaseApplication.getInstance(), resourceId, Toast.LENGTH_LONG).show();
    }

    public static void shortToast(String content) {
        Toast.makeText(BaseApplication.getInstance(), content, Toast.LENGTH_SHORT).show();
    }

    public static void shortToast(int resourceId) {
        Toast.makeText(BaseApplication.getInstance(), resourceId, Toast.LENGTH_SHORT).show();
    }

    public static void centerLongToast( Context context , String content ){
        Toast toast = Toast.makeText( context, content , Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }



    protected static Toast toast   = null;
    private static String oldMsg;
    private static long oneTime = 0;
    private static long twoTime = 0;
    public static void showToastShort(Context context, int resId){
        showToastShort(context, context.getString(resId));
    }

    public static void showToastShort(Context context, String s){
        if(toast==null){
            toast =Toast.makeText(context, s, Toast.LENGTH_SHORT);
            toast.show();
            oneTime=System.currentTimeMillis();
        }else{
            twoTime=System.currentTimeMillis();
            if(s.equals(oldMsg)){
                if(twoTime-oneTime>Toast.LENGTH_SHORT){
                    toast.show();
                }
            }else{
                oldMsg = s;
                toast.setText(s);
                toast.show();
            }
        }
        oneTime=twoTime;
    }



    /*--------------------------------自定义toast逻辑处理----------------------------------*/

//    public static Toast toast;
//
//    private static void createToast(Context context, CharSequence msg, int duration){
//        View view = LayoutInflater.from(context).inflate(R.layout.toast_layout,null);
//        TextView textView = (TextView) view.findViewById(R.id.content);
//        textView.setText(msg);
//        toast = new Toast(context);
//        toast.setDuration(duration);
//        toast.setGravity(Gravity.BOTTOM, 0, ScreenUtils.dip2px(context,50));
//        toast.setView(view);
//        toast.show();
//    }
//
//    public static void showToastLong(final Context context, final String msg) {
//        if (!TextUtils.isEmpty(msg)) {
//            ThreadUtil.runOnUiThread(new Runnable() {
//                @Override
//                public void run() {
//                    createToast(context,msg,Toast.LENGTH_LONG);
//                }
//            });
//        }
//    }
//
//    public static void showToastLong(final Context context, final int msgId) {
//        if (msgId != 0) {
//            ThreadUtil.runOnUiThread(new Runnable() {
//                @Override
//                public void run() {
//                    createToast(context,context.getResources().getString(msgId),Toast.LENGTH_LONG);
//                }
//            });
//        }
//    }
//
//    public static void showToastShort(final Context context, final String msg) {
//        if (!TextUtils.isEmpty(msg)) {
//            ThreadUtil.runOnUiThread(new Runnable() {
//                @Override
//                public void run() {
//                    createToast(context,msg,Toast.LENGTH_SHORT);
//                }
//            });
//        }
//    }
//
//    public static void showToastShort(final Context context, final int msgId) {
//        if (msgId > 0) {
//            ThreadUtil.runOnUiThread(new Runnable() {
//                @Override
//                public void run() {
//                    createToast(context,context.getResources().getString(msgId),Toast.LENGTH_SHORT);
//                }
//            });
//        }
//    }
}
